<template>
  <div class="app-content-wrap" :style="'minHeight:' + gHeight + 'px;'">
    <div class="g-content-title">
      巡更点位
      <div class="department-top-btn">
        <el-button class="btn-default" type="primary" plain icon="el-icon-plus" size="mini" @click="createPoint">新建点位</el-button>
      </div>
    </div>
    <!-- 搜索框 -->
    <el-form ref="patrolFilter" :model="patrolFilter" label-width="80px" size="small">
      <el-row :gutter="15">
        <el-col :span="11">
          <el-form-item label="社区">
            <AreaSelect :selectModel="patrolFilter.deptId" :areaIds="areaIdsNew" @onAreaChange="onAreaChange" hideThird="true">
            </AreaSelect>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item>
            <el-button type="primary" size="small" icon="el-icon-search" @click="getPartolFromFilter">搜 索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 列表 -->
    <div style="margin:10px 30px;">
      <el-table :data="patrolListTable" border style="width: 100%">
        <el-table-column prop="pointCode" label="点位编码" min-width="140" align="center"></el-table-column>
        <el-table-column prop="pointName" label="点位名称" min-width="100" align="center"></el-table-column>
        <el-table-column prop="pointAddr" label="位置" min-width="120" align="center"></el-table-column>
        <el-table-column prop="gpsLng" label="经度" min-width="140" align="center"></el-table-column>
        <el-table-column prop="gpsLat" label="纬度" min-width="140"></el-table-column>
        <el-table-column label="是否有效" min-width="60" align="center">
          <template slot-scope="scope">
            <el-tag size="small" :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? '是' : '否' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="二维码" min-width="80" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.codePath" alt="" width="50" height="50" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="190" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="pointEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="pointDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination background @current-change="handleCurrentChange" :current-page.sync="pagination.currentPage" :page-size="10" layout="total, prev, pager, next,jumper" :total="pagination.total" class="el-page-body"></el-pagination>
    </div>
    <el-dialog title="新建点位" :visible.sync="newPatrolVisible" :close-on-click-modal="false">
      <el-form ref="newPatrolForm" :model="newPatrolForm" :rules="patrolRules" label-width="80px">
        <el-row :gutter="15">
          <el-form-item label="选择小区" prop="deptId">
            <AreaSelect :selectModel="newPatrolForm.deptId" :areaIds="areaIds" @onAreaChange="onAreaChangeNew" hideThird="true">
            </AreaSelect>
          </el-form-item>
        </el-row>
        <el-form-item label="点位编码">
          <el-input v-model="newPatrolForm.pointCode"></el-input>
        </el-form-item>
        <el-form-item label="点位名称">
          <el-input v-model="newPatrolForm.pointName"></el-input>
        </el-form-item>
        <el-form-item label="所在位置">
          <el-input v-model="newPatrolForm.pointAddr"></el-input>
        </el-form-item>
        <el-row class="input_gps">
          <el-col>
            <el-col :span="6">
              <el-form-item prop="lng" label="经纬度">
                <el-input v-model="newPatrolForm.gpsLng"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item class="item-inline" prop="lat">
                <el-input v-model="newPatrolForm.gpsLat"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="item-inline-left" :span="2">
              <el-button type="text" icon="el-icon-location" @click="showMap">选择地图位置</el-button>
            </el-col>
          </el-col>
        </el-row>
        <el-form-item label="是否有效">
          <el-radio v-model="newPatrolForm.status" :label="1">是</el-radio>
          <el-radio v-model="newPatrolForm.status" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addPatrol()">提 交</el-button>
        </el-form-item>
      </el-form>
      <el-dialog top="10vh" custom-class="pic-popup" :visible.sync="mapVisible" append-to-body>
        <mapSelect :LngLat="lngLat" :MapClick="mapClick"></mapSelect>
      </el-dialog>
    </el-dialog>
  </div>
</template>
<script>
import AreaSelect from '../../components/common/select/area.vue'
import mapSelect from '../../components/common/ui/mapSelect'

export default {
  name: 'appletManager',
  components: {
    AreaSelect,
    mapSelect
  },
  data () {
    return {
      gHeight: this.$store.state.gHeight,
      areaIds: {
        areaId1: '',
        areaId2: '',
        areaId3: ''
      },
      areaIdsNew: {
        areaId1: '',
        areaId2: '',
        areaId3: ''
      },
      patrolFilter: {
        userName: '', // 姓名
        mobile: '', // 电话
        deptId: '' // 社区
      },
      newPatrolForm: {
        deptId: '', // 社区
        pointCode: '',
        pointName: '',
        pointAddr: '',
        gpsLng: '',
        gpsLat: '',
        mapType: 0,
        status: 0 // 是否有效
      },
      flag: true, // true:新建, false:编辑
      lngLat: {}, // 地图标注点的位置
      newPatrolVisible: false,
      mapVisible: false,
      mapClick: true, // 地图关闭按钮是否显示
      patrolListTable: [],
      pagination: {
        currentPage: 1,
        total: null
      },
      patrolRules: {
        deptId: [
          { required: true, message: '请选择小区', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    pointData () {
      return this.$store.state.event.pointData
    },
    pointPopShow () {
      return this.$store.state.event.pointPopShow
    }
  },
  watch: {
    pointData: {
      handler (curVal) {
        this.newPatrolForm.gpsLng = curVal.lng
        this.newPatrolForm.gpsLat = curVal.lat
      },
      deep: true
    },
    pointPopShow: {
      handler (curVal) {
        this.mapVisible = curVal
      },
      deep: true
    }
  },
  methods: {
    createPoint () {
      this.flag = true
      this.newPatrolVisible = true
    },
    addPatrol () {
      this.$refs.newPatrolForm.validate((valid) => {
        if (valid) {
          if (this.flag) {
            const url = this.$base.getUrl('patrolpointAdd')
            let params = {
              isJson: true,
              url,
              data: this.newPatrolForm
            }
            this.$store.dispatch('postForm', params).then((res) => {
              if (res.code === 0) {
                this.$message({
                  message: '新建点位成功!',
                  type: 'success'
                })
                this.newPatrolVisible = false
                this.newPatrolForm = {}
                this.areaIdsNew = {}
                this.getPartolList()
              }
            })
          } else {
            const url = this.$base.getUrl('patrolpointEdit')
            let params = {
              isJson: true,
              url,
              data: this.newPatrolForm
            }
            this.$store.dispatch('postForm', params).then((res) => {
              if (res.code === 0) {
                this.$message({
                  message: '更新点位成功!',
                  type: 'success'
                })
                this.newPatrolVisible = false
                this.newPatrolForm = {}
                this.areaIdsNew = {}
                this.getPartolList()
              }
            })
          }
        }
      })
    },
    showMap () {
      if (!this.newPatrolForm.deptId) {
        this.$message({
          type: 'warning',
          message: '请选择小区'
        })
        return
      }

      let _this = this
      let url = this.$base.getUrl('getDeptById')
      let params = {
        url: url,
        data: {
          deptId: _this.newPatrolForm.deptId
        }
      }
      _this.lngLat = {}
      this.$store.dispatch('getForm', params).then(res => {
        this.newPatrolForm.gpsLng = res.gpsLng
        this.newPatrolForm.gpsLat = res.gpsLat
        _this.lngLat = {
          lng: res.gpsLng || 121.501472, // 121.501472,31.233027
          lat: res.gpsLat || 31.233027
        }
        _this.mapVisible = true
        _this.$store.commit('setPointPopShow', true)
      })
    },
    pointEdit (row) {
      this.newPatrolForm = row
      this.flag = false
      this.newPatrolVisible = true
    },
    // 删除点位
    pointDelete (row) {
      let id = row.id
      this.$confirm('删除该巡更点位?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleRemovePoint(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleRemovePoint (id) {
      const url = this.$base.getUrl('patrolpointDelete')
      this.$store.dispatch('postForm', {
        isJson: true,
        url: url,
        data: {
          id: id
        }
      }).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getPartolList()
        }
      })
    },
    getPartolFromFilter: function () { /// /表单
      this.pagination.currPage = 1
      this.getPartolList()
    },
    handleCurrentChange (currPage) {
      this.pagination.currentPage = currPage
      this.getPartolList()
    },
    getPartolList () {
      const url = this.$base.getUrl('patrolpointList')
      let params = {
        isJson: true,
        url: url,
        data: {
          deptId: this.patrolFilter.deptId, // 部门
          page: this.pagination.currentPage,
          limit: 10
        }
      }
      this.$store.dispatch('postForm', params).then((res) => {
        let { result } = res
        this.patrolListTable = result.list
        this.pagination.currentPage = result.currPage
        this.pagination.total = result.totalCount
      })
    },
    // area组件change时
    onAreaChange (val) {
      this.patrolFilter.deptId = val
    },
    onAreaChangeNew (val) {
      this.newPatrolForm.deptId = val
    }
  },
  filters: {

  },
  mounted () {
    this.getPartolList()
  }
}
</script>
<style lang='less' scoped>
.g-content-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.department-top-btn {
  float: right;
}

.department-top-btn .btn-default {
  display: inline-block;
  padding: 2px 12px;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.42857143;
  text-align: center;
  cursor: pointer;
  background: none;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-right: 30px;
}
.el-table {
  width: auto !important;
  text-align: center;
  margin: 10px;
}

/deep/ .el-table th {
  text-align: center;
}
.el-dialog__wrapper /deep/ .pic-popup {
  position: absolute;
  top: 30%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: auto;
  margin: 0;
}
</style>
